package com.example.sports_meet.athlete;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;

import java.util.Objects;

public class AthleteQuery extends AppCompatActivity implements View.OnClickListener {

    private EditText etID, etName;
    private TextView mTvshow;
    private String eno1, eno2;
    private Button search1, search2;
    //下拉框
    private Spinner spGameType, spGame;
    private MyDbHelper myDbHelper;
    private TextView tvInfo;
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_athlete_query);
        Objects.requireNonNull(getSupportActionBar()).hide();
        ImageButtonInit();
        spinnerinit();
        myDbHelper = new MyDbHelper(this, "db", null, 2);
        search1 = findViewById(R.id.btn_queryone);
        search2 = findViewById(R.id.btn_querytwo);
        mTvshow = findViewById(R.id.tv_selected);
        search1.setOnClickListener(this);
        search2.setOnClickListener(this);

    }

    public void onClick(View v) {
        SQLiteDatabase db;

        switch (v.getId()) {
            case R.id.btn_queryone:
                db = myDbHelper.getReadableDatabase();
                mTvshow.setText("");
                Cursor cursor = db.query("signup", new String[]{"ano", "aname", "eno"}, "eno=? OR eno=?", new String[]{eno1, eno2}, null, null, null);
                if (cursor.getCount() == 0) {
                    Toast.makeText(this, "暂未查询到报名结果", Toast.LENGTH_SHORT).show();
                } else {
                    cursor.moveToFirst();
                    mTvshow.setText("学号：" + cursor.getString(0) + "   姓名：" + cursor.getString(1));
                    while (cursor.moveToNext()) {
                        mTvshow.append("\n" + "学号：" + cursor.getString(0) + "   姓名：" + cursor.getString(1));
                    }
                }
                cursor.close();
                db.close();
                break;
            case R.id.btn_querytwo:
                db = myDbHelper.getReadableDatabase();
                mTvshow.setText("");
                String ano = etID.getText().toString();
                String aname = etName.getText().toString();
                Cursor cursor2 = db.query("signup", new String[]{"ano", "aname", "eno", "ename"}, "ano=? AND aname=?", new String[]{ano, aname}, null, null, null);

                if (cursor2.getCount() == 0) {
                    Toast.makeText(this, "暂未查询到报名结果", Toast.LENGTH_SHORT).show();
                } else {
                    cursor2.moveToFirst();
                    mTvshow.setText("比赛编号：" + cursor2.getString(2) + "\n" + "项目名称：" + cursor2.getString(3));
                    while (cursor2.moveToNext()) {
                        mTvshow.append("\n" + "比赛编号：" + cursor2.getString(2) + "\n" + "项目名称：" + cursor2.getString(3));
                    }
                }
                cursor2.close();
                db.close();
                break;
        }

    }

    //内容清空Imagebutton
    public void ImageButtonInit() {
        etID = findViewById(R.id.etAthleteID);
        ImageButton deleteOneButton = findViewById(R.id.delete_one);

        deleteOneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etID.setText("");
            }
        });

        etName = findViewById(R.id.etName);
        ImageButton deleteTwoButton = findViewById(R.id.delete_two);

        deleteTwoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etName.setText("");
            }
        });
    }

    //项目选择下拉框联动
    public void spinnerinit() {
        spGameType = (Spinner) findViewById(R.id.spinnerOne);
        spGame = (Spinner) findViewById(R.id.spinnerTwo);

        adapter = ArrayAdapter.createFromResource(AthleteQuery.this, R.array.game_type, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spGameType.setAdapter(adapter);
        spGame.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Spinner spinner = (Spinner) parent;

                String spGame = (String) spinner.getItemAtPosition(position);
                if (spGame.equals("100米自由泳")) {
                    eno1 = "202204001";
                    eno2 = "202204002";
                } else if (spGame.equals("100米仰泳")) {
                    eno1 = "202204005";
                    eno2 = "202204006";
                } else if (spGame.equals("水上芭蕾")) {
                    eno1 = "202204003";
                    eno2 = "2022004004";
                } else if (spGame.equals("10米气手枪")) {
                    eno1 = "202204007";
                    eno2 = "202204008";
                } else if (spGame.equals("飞碟多向")) {
                    eno1 = "202204010";
                    eno2 = "202204009";
                } else if (spGame.equals("25米手枪速射")) {
                    eno1 = "202204011";
                    eno2 = "202204012";
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spGameType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Spinner spinner = (Spinner) parent;
                String gameType = (String) spinner.getItemAtPosition(position);
                ArrayAdapter<CharSequence> gameAdapter = ArrayAdapter.createFromResource(AthleteQuery.this, R.array.ssxm, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
                spGame.setAdapter(gameAdapter);
                if (gameType.equals("水上项目")) {
                    gameAdapter = ArrayAdapter.createFromResource(AthleteQuery.this, R.array.ssxm, android.R.layout.simple_spinner_item);
                    spGame.setAdapter(gameAdapter);
                } else if (gameType.equals("射击类")) {
                    gameAdapter = ArrayAdapter.createFromResource(AthleteQuery.this, R.array.sjl, android.R.layout.simple_spinner_item);
                    spGame.setAdapter(gameAdapter);
                } else if (gameType.equals("田径")) {
                    gameAdapter = ArrayAdapter.createFromResource(AthleteQuery.this, R.array.tj, android.R.layout.simple_spinner_item);
                    spGame.setAdapter(gameAdapter);
                } else if (gameType.equals("冰上运动")) {
                    gameAdapter = ArrayAdapter.createFromResource(AthleteQuery.this, R.array.bsyd, android.R.layout.simple_spinner_item);
                    spGame.setAdapter(gameAdapter);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}